const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workshop:{},
    userInfo:{},
    workshopRecommend:[],
    slider_count: 1,
    indicatorDots: false,
    current:1,
    user:'',
    mobile:'',
    qrcode:'',
    showDialog: false,
    show:false,
    button_disabled:false,
    button_loading:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let workshop_id=''
    let agent_id=''
    if (app.globalData.user && app.globalData.user != null) {
      agent_id=app.globalData.user.id
      //获取用户信息
      this.setData({
        user: app.globalData.user,
        mobile: app.globalData.user.mobile
      })
    } else {
      app.emptyUserCallback = user => {
        if (user != null) {
          this.setData({
            user: app.globalData.user,
            mobile: app.globalData.user.mobile
          });

        }
      }
    }
    if (app.globalData.userInfo && app.globalData.userInfo != null) {
      let user_id = app.globalData.userInfo.user_id
      if (options.scene) {
        let scene = decodeURIComponent(options.scene);
        //&是我们定义的参数链接方式
        user_id = scene.split("&")[0];
        console.log(user_id)
         workshop_id = scene.split('&')[1];
      }
      if (options.user_id) {
        user_id = options.user_id
      }
      console.log('用户', user_id)
      if (user_id != app.globalData.userInfo.user_id) {
        wx.setStorageSync('user_id', user_id)
        //推荐个人
        wx.request({
          url: app.globalData.apiurl + "agent/" + user_id,
          /*  header: {
              "Authorization": "bearer " + app.globalData.access_token
            },*/
          method: 'get',
          success: res => {
            if (res.data.code == 0) {
              app.globalData.userInfo = res.data.data
              this.setData({
                userInfo: app.globalData.userInfo
              })
            }
          }
        })
      } else {

        //获取用户信息
        this.setData({
          userInfo: app.globalData.userInfo
        })
        wx.setNavigationBarTitle({
          title: '昊信地产-' + app.globalData.userInfo.name + '的名片',
        })
      }



    } else {

      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.emptyUserInfoCallback = userInfo => {
        if (userInfo != null) {
          this.setData({
            userInfo: app.globalData.userInfo
          });
          wx.setNavigationBarTitle({
            title: '昊信地产-' + app.globalData.userInfo.name + '的名片',
          })
        }

      }
    }
    if (options.scene) {
      let scene1 = decodeURIComponent(options.scene);
     
      workshop_id = scene1.split('&')[1];
    }
    if(options.workshop_id){
      workshop_id = options.workshop_id
    }
    wx.request({
      url: app.globalData.apiurl+'workshop/'+workshop_id,
      method: 'get',
      success: res => {
        if (res.data.code == 0) {
          this.setData({
            workshop: res.data.data,
            slider_count: Object.keys(res.data.data.slider).length,
            current:1
          })
          console.log(res.data.data.slider)
          var article = res.data.data.desc;
          /**
          * WxParse.wxParse(bindName , type, data, target,imagePadding)
          * 1.bindName绑定的数据名(必填)
          * 2.type可以为html或者md(必填)
          * 3.data为传入的具体数据(必填)
          * 4.target为Page对象,一般为this(必填)
          * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
          */
          var that = this;
          WxParse.wxParse('article', 'html', article, that, 5);
          wx.setNavigationBarTitle({
            title: res.data.data.title.substring(0,13)+'...',
          })
        }
      },
    })
      //推荐房源请求
      wx.request({
        url: app.globalData.apiurl + "workshop_detail_recommend",
        /*  header: {
            "Authorization": "bearer " + app.globalData.access_token
          },*/
        data:{workshop_id:workshop_id},
        method: 'get',
        success: res => {
          if (res.data.code == 0) {
            this.setData({
              workshopRecommend: res.data.data
            })
          }
        },
        fail: function () {
        }
      })
 
  },
  //分享
  onShareAppMessage: function (options) {
    return {
      title: this.data.workshop.title,
      imageUrl: this.data.workshop.logo,
      path: 'pages/detail/detail?workshop_id='+this.data.workshop.workshop_id+'&user_id=' + this.data.userInfo.user_id
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

 
  /**
   * 预览图片
   */
  previewImage: function (e) {
    console.log(this.data.workshop)
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.workshop.slider // 需要预览的图片http链接列表  
    })
  },
    //地图
  callMap() {
    
    wx.openLocation({//​使用微信内置地图查看位置。
      latitude: parseFloat(this.data.workshop.lat),//要去的纬度-地址
      longitude: parseFloat(this.data.workshop.lng),//要去的经度-地址
      name: this.data.workshop.address,
      address: this.data.workshop.address
    })
  },
  //电话
  makeCall: function (e) {
    console.log(this.data.userInfo.mobile)
    wx.makePhoneCall({
      phoneNumber: this.data.userInfo.mobile
    })
  },
  //绑定slider
  bindchange:function(e){
      console.log(e)
      this.setData({
        current:e.detail.current+1
      })
  },
  //跳转预约看厂页面
  yuyue:function(e){
    let workshop_id = e.currentTarget.dataset.detail
    if(!this.data.mobile){
      let url = '/pages/yuyue/index?workshop_id='+workshop_id
      wx.navigateTo({
        url: '/pages/mobile/mobile?url=' + encodeURIComponent(url),
      })
      return ;
    }
  
    wx.navigateTo({
      url: '/pages/yuyue/index?workshop_id='+workshop_id,
    })
  },
  getPhoneNumber(e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    console.log(1111)
    wx.request({
      url: app.globalData.apiurl + 'jiemi',
      method: "GET",
      data: {
        iv: e.detail.iv,
        data: e.detail.encryptedData,
        openid: this.data.user.openid
      },
      success: (res) => {
        if (res.data.code == 0) {
          app.globalData.user = res.data.data
          this.setData({
            user: res.data.data,
            mobile: res.data.data.mobile,
          })
       
          wx.navigateTo({
            url: '/pages/yuyue/index?workshop_id=' +this.data.workshop.workshop_id,
          })
        } else {
          console.log('失败')
        }
      }

    })
  },
   //跳转投诉看厂页面
  bindtousu: function (e) {
    let url = '/pages/tousu/tousu'
    if (!this.data.mobile) {
      wx.navigateTo({
        url: '/pages/mobile/mobile?url=' + encodeURIComponent(url),
      })
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },
  //分享朋友圈
  fenxiang:function(e){
    this.toggleDialog()

    if(this.data.qrcode==''){
      this.setData({
        show:true,

        button_disabled:true
      })
      wx.request({
        url: app.globalData.apiurl + "workshopQrcode",
        method: 'get',
        data: { workshop_id: this.data.workshop.workshop_id, agent_id: this.data.userInfo.user_id},
        success:res=>{
          console.log(res)
          if(res.data.code==0){
            this.setData({
              qrcode:res.data.data,
              show: false,
    
              button_disabled: false
            })
          }
        }
      })
    }
  }  ,
  //保存图片
  baocun:function(e){
    this.setData({
      button_loading: true,
    })

    wx.downloadFile({
      url:this.data.qrcode,
      success:res=>{
        if(res.statusCode==200){
          let img=res.tempFilePath
          wx.saveImageToPhotosAlbum({
            filePath: img,
            success:res=>{
              this.setData({
                button_loading: false,
              })
              wx.showToast({title:'保存成功'}) 
              this.toggleDialog()
             
            },
            fail:res=>{
              this.setData({
                button_loading: false,
              })
              wx.showToast({ title: '保存失败' }) 
            }
          })
        }
      }
    })
  },
  //弹窗
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
})